package cn.elead.chaos.sso.server.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import cn.elead.chaos.api.enums.ExStatus;
import cn.elead.chaos.api.web.IExceptionResolverService;
import cn.elead.chaos.sso.core.constant.SsoConstant;
import lombok.extern.slf4j.Slf4j;

/**
 * 异常处理
 * 
 * @author luopeng
 *
 */
@Slf4j
public class OauthExceptionResolverService implements IExceptionResolverService {

	@Override
	public ExStatus doException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex,  Object view) {
		if (ex instanceof OauthApiException) {
			view = new ModelAndView();
			ModelAndView model = (ModelAndView) view;
			// 进入登录页面
			model.addObject(SsoConstant.PARAM_CLIENT_ID, request.getParameter(SsoConstant.PARAM_CLIENT_ID));
			model.addObject(SsoConstant.PARAM_RESPONSE_TYPE, request.getParameter(SsoConstant.PARAM_RESPONSE_TYPE));
			model.addObject(SsoConstant.REDIRECT_URI, request.getParameter(SsoConstant.REDIRECT_URI));
			log.error(ex.getMessage(), ex);
			return ExStatus.RETURN;
		}
		return ExStatus.HIT;
	}

}
